function toRGB( color ) {
  const rgb = [0, 0, 0];

  if( color.startsWith("#") ) {
    color = color.slice(1);
  }
  else if( color.startsWith("0x") ){
    color = color.slice(2);
  }

  if( color.length === 3 ) {
    for( let i = 0, il = rgb.length; i < il; i++ ) {
      rgb[i] = parseInt( color[i], 16 );
    }
  }
  else if( color.length === 6 ) {
    for( let i = 0, il = rgb.length; i < il; i++ ) {
      rgb[i] = parseInt( color.substr(i*2, 2), 16 );
    }
  }

  return rgb;

}

function toColor( rgb ) {
  var c = "#";
  for( var i = 0; i < 3; i++ ) {
    c += rgb[i] < 16 ? `0${rgb[i].toString(16)}` : rgb[i].toString(16);
  }
  return c;
}

function gradientColor( start, end, step ) {
  const startColor = toRGB( start );
  const endColor = toRGB( end );
  let colors = [];

  step = step || 3;

  const steps = endColor.map((value,index)=>(value-startColor[index])/(step - 1));

  for( let i = 0; i < step; i++ ) {
    colors[i] = startColor.map( (value, index) =>Math.floor(value + steps[index] * i));
  }

  // colors[step-1] = [...endColor];

  colors = colors.map(value=>toColor(value));

  return colors;

}

export{
  toColor,
  toRGB,
  gradientColor,
}